import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import { GameEvent } from "../libs/GameEvent";
import Helper from "../libs/HelperMgr";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;



@ccclass
export default class Player extends cc.Component {



  @property(cc.Node)
  rod: cc.Node = null;




  onEnable() {
    this.rod.getComponent(cc.BoxCollider).enabled = false;
    GameEvent.on(Constants.StageEvent.RotateRod, this.rotateRod, this)
    GameEvent.on(Constants.StageEvent.LaunchRod, this.launchRod, this)
    GameEvent.on(Constants.StageEvent.ResetRod, this.resetRod, this)
    GameEvent.on(Constants.StageEvent.ChangePlayerAni, this.changeState, this)


  }

  start(){
    this.changeRod();
    this.setInitPos();
  }

  setInitPos() {
    let groundInfo = Constants.StageInfo[GameData.instance.gameStage-1].ground;
    this.node.setPosition(groundInfo.pos.x , groundInfo.pos.y + groundInfo.offset)
}


  update(dt: number): void {
    if(GameManager.instace.gameIsStart && !GameManager.instace.gameIsOver){
      this.rod.active = !GameManager.instace.playerIsStosh;
    }
      
  }

  async changeState(state:string){
    let spr = this.node.getChildByName("ani").getComponent(cc.Sprite)
    let waitsec = 0
    if(spr){
      switch (state) {
        case Constants.PlayerAniState.stosh:
            waitsec = Helper.accumulationNumber(12);
            for (let i = 1; i < 12; i++) {
              this.scheduleOnce(async ()=>{
                spr.spriteFrame = await Helper.loadBundle(`stosh/${i}`,cc.SpriteFrame) 
              },0.01*i)
            }
          break;
      
        default:
          break;
      }
      this.scheduleOnce(async ()=>{
        spr.spriteFrame = await Helper.loadBundle(`stosh/${1}`,cc.SpriteFrame) 
      },0.01*waitsec) 
    }
  }


  async changeRod(){
    if(GameData.instance.playRod > 0){
      let img = this.rod.getChildByName("img").getComponent(cc.Sprite)
      img.spriteFrame = await Helper.loadRes(`image/rod${GameData.instance.playRod}`, cc.SpriteFrame)
    }
     
  }

  resetRod(){
    this.rod.angle = 0;
    GameManager.instace.playerIsWave = false;
    this.rod.getComponent(cc.BoxCollider).enabled = false;
  }



  rotateRod(angle) {
    GameManager.instace.playerIsWave = true;
    this.rod.getComponent(cc.BoxCollider).enabled = false;
    this.rod.angle = - (angle * this.node.scaleX + 90);
  }

  launchRod() {
    if(GameManager.instace.gameChance > 0){
      GameData.instance.gameModel && GameManager.instace.gameChance --;
      let speed = GameManager.instace.forcePercent * Constants.RodInfo[GameData.instance.playRod].speed;
      this.rod.getComponent(cc.BoxCollider).enabled = true;
      cc.tween(this.rod).to(10 / speed, { angle: 0 }).call(() => {
        GameManager.instace.playerIsWave = false;
        this.rod.getComponent(cc.BoxCollider).enabled = false;
      }).start()
    }
   
  }


  onDisable() {
    GameEvent.off(Constants.StageEvent.RotateRod, this.rotateRod, this)
    GameEvent.off(Constants.StageEvent.LaunchRod, this.launchRod, this)
    GameEvent.off(Constants.StageEvent.ResetRod, this.resetRod, this)
    GameEvent.off(Constants.StageEvent.ChangePlayerAni, this.changeState, this)

  }
}
